using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Net.Sockets;

namespace Microsoft.Samples.BizTalk.Adapter.Tcp.Receive
{
    /// <summary>
    ///     This class handles socket errors for the receive adapter.
    /// </summary>
    /// <author>
    ///     Paul Brown
    /// </author>
    public class SocketErrorHandler
    {
        #region Public Static Methods

        /// <summary>
        ///     This method handles the socket error depending on the type of error.
        /// </summary>
        public static void HandleError(ReceiveAdapter adapter, ReceiveLocation receiveLocation, SocketException e, bool shutdownReceiveLocationIfNotHandled)
        {
            // Switch the SocketErrorCode to handle each one individually
            switch (e.SocketErrorCode)
            {
                case SocketError.Interrupted :

                    // Do nothing - a blocking call was cancelled likely because the socket was closed
                    break;

                default :

                    TraceHelper.WriteEntry(TraceLevel.Error, ResourceConstants.ReceiveSocketError, e.Message);

                    if (shutdownReceiveLocationIfNotHandled)
                    {
                        // Socket error hasn't been handled, shut receive location down
                        adapter.TransportProxy.ReceiverShuttingdown(receiveLocation.Uri, e);
                    }
                    break;
            }
        }

        #endregion
    }
}
